using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DreamBuilder.UI.UserControls.DataAccess;
using DreamBuilder.UI.UserControls.DataAccess.Services;



namespace DreamBuilder.UI.UserControls
{
    public class ChildItemAdd
    {
        public string ChildEditID
        {
            get;
            set;
        }
        public ChildItemAdd()
        {
           
        }
        public void AddNewRow(ref DataTable dtInfo)
        {
            DataRow drNew = dtInfo.NewRow();
            string childPKField = UChildCommon.GetChildTablePKField(ChildEditID);
            drNew[childPKField] = System.Guid.NewGuid().ToString();
            drNew["RowStatus"] = 1;
            drNew["RowAttribute"] = "Ordinary";
            dtInfo.Rows.Add(drNew);
        }



        private bool CheckCodeRepeat(string code, DataTable dtInfo)
        {
            bool existed = false;
            string fieldCode = UChildCommon.GetBusySelectField(ChildEditID);
            if (dtInfo.Rows.Count > 0)
            {
                foreach (DataRow drInfo in dtInfo.Rows)
                {
                    if (drInfo.RowState != DataRowState.Deleted)
                    {
                        if (drInfo["" + fieldCode + ""].ToString() == code)
                        {
                            existed = true;
                            break;
                        }
                    }
                }
            }
            else
            {
                existed = false;
            }
            return !existed;
        }

        public void AddNewRow(ref DataTable dtInfo, string code,bool repeatEnable)
        {
            bool canAdd = true;
            if (repeatEnable == false)
            {
                canAdd = CheckCodeRepeat(code, dtInfo);
            }
            if (canAdd == true)
            {
                DataRow drNew = dtInfo.NewRow();
                string childPKField = UChildCommon.GetChildTablePKField(ChildEditID);
                string busySelectField = UChildCommon.GetBusySelectField(ChildEditID);
                drNew[childPKField] = System.Guid.NewGuid().ToString();
                drNew[busySelectField] = code;
                drNew["RowStatus"] = 1;
                drNew["RowAttribute"] = "Ordinary";
                dtInfo.Rows.Add(drNew);
            }
        }
    }
}
